package com.itheima.service.mongo.impl;

import com.itheima.domain.mongo.Friend;
import com.itheima.domain.mongo.RecommendUser;
import com.itheima.domain.mongo.UserLike;
import com.itheima.domain.mongo.Visitor;
import com.itheima.service.mongo.FriendService;
import com.itheima.service.mongo.UserLikeSrevice;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.UserLikeCountVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.http.ResponseEntity;

import java.util.ArrayList;
import java.util.List;

@DubboService
public class UserLikeSreviceImpl implements UserLikeSrevice {

    @Autowired
    MongoTemplate mongoTemplate;

    @Autowired
    FriendService friendService;


    @Override
    public UserLikeCountVo counts(Long userId) {
        UserLikeCountVo userLikeCountVo = new UserLikeCountVo();
        Query query = new Query(Criteria.where("userId").is(userId));
        long eachLoveCount = mongoTemplate.count(query, Friend.class);
        Query query1 = new Query(Criteria.where("userId").is(userId));
        long loveCount = mongoTemplate.count(query1, UserLike.class);
        Query query2 = new Query(Criteria.where("likeUserId").is(userId));
        long fanCount = mongoTemplate.count(query2, UserLike.class);
        userLikeCountVo.setEachLoveCount(eachLoveCount);
        userLikeCountVo.setLoveCount(loveCount);
        userLikeCountVo.setFanCount(fanCount);
        return userLikeCountVo;
    }

    @Override
    public PageBeanVo LikeByPage(String type, Long userId, Integer pageNum, Integer pageSize) {
        PageBeanVo pageBeanVo = new PageBeanVo();
        switch (type) {
            case "1": {//互相关注
                Query query = new Query(Criteria.where("userId").is(userId)).skip((pageNum - 1) * pageSize).limit(pageSize);
                List<Friend> friendList = mongoTemplate.find(query, Friend.class);
                List<Long> list = new ArrayList<>();
                friendList.forEach(friend -> {
                    list.add(friend.getFriendId());
                });
                long count = mongoTemplate.count(query, Friend.class);
                return new PageBeanVo(pageNum, pageSize, count, list);

            }

            case "2": {//我关注
                Query query = new Query(Criteria.where("userId").is(userId)).skip((pageNum - 1) * pageSize).limit(pageSize);
                List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
                long count = mongoTemplate.count(query, UserLike.class);
                List<Long> list = new ArrayList<>();
                userLikes.forEach(user -> {
                    list.add(user.getLikeUserId());
                });
                return new PageBeanVo(pageNum, pageSize, count, list);
            }

            case "3": {//喜欢我的
                Query query = new Query(Criteria.where("likeUserId").is(userId)).skip((pageNum - 1) * pageSize).limit(pageSize);
                List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
                long count = mongoTemplate.count(query, UserLike.class);
                List<Long> list = new ArrayList<>();
                userLikes.forEach(user -> {
                    list.add(user.getUserId());
                });
                return new PageBeanVo(pageNum, pageSize, count, list);
            }
            case "4": {//谁看过我
                //构造条件
                Query query = Query.query(Criteria.where("userId").is(userId))
                        .with(Sort.by(Sort.Order.desc("date")))
                        .skip((pageNum - 1) * pageSize).limit(pageSize);
                //查询来客
                List<Visitor> visitors = mongoTemplate.find(query, Visitor.class);
                long count = mongoTemplate.count(query, Visitor.class);
                List<Long> list = new ArrayList<>();
                visitors.forEach(user -> {
                    list.add(user.getVisitorUserId());
                });

                return new PageBeanVo(pageNum, pageSize, count, list);

            }
        }
        return null;
    }

    @Override
    public Boolean saveLove(UserLike userLike) {
        // 1. 保存喜欢
        mongoTemplate.save(userLike);
        // 2. 查询是否对方喜欢自己
        Query query = new Query(Criteria.where("userId").is(userLike.getLikeUserId()).and("likeUserId").is(userLike.getUserId()));
        boolean exists = mongoTemplate.exists(query, UserLike.class);
        return exists;
    }

    @Override
    public Boolean removeLove(Long userId, Long likeUserId) {
        // 1. 删除喜欢
        Query query = new Query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId));
        mongoTemplate.remove(query, UserLike.class);
        // 2. 查询对方是否喜欢自己
        Query query1 = new Query(Criteria.where("userId").is(likeUserId).and("likeUserId").is(userId));
        boolean exists = mongoTemplate.exists(query1, UserLike.class);
        return exists;
    }

    @Override
    public void remove(Long userId, Long likeUserId) {
        mongoTemplate.remove(Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId)), RecommendUser.class);
    }


}
